//
//  SoapViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/8/31.
//  Copyright (c) 2015年 ltz. All rights reserved.
//
#import "AFNetworking.h"
#import "SoapViewController.h"
#import "GDataXMLNode.h"
@interface SoapViewController ()

@end

@implementation SoapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationTitle:@"查询手机号归属地" barTintColor:0x1E86CB titleColor:0xFFFFFF isTranslucent:NO isHidden:NO];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xFFFFFF];}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)click:(id)sender {
    NSString *soapMsg;
    if (_phoneText.text.length!=0) {
        
        
        soapMsg= [NSString stringWithFormat:
                            @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                            "<soap12:Envelope "
                            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                            "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
                            "<soap12:Body>"
                            "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
                            "<mobileCode>%@</mobileCode>"
                            "<userID>%@</userID>"
                            "</getMobileCodeInfo>"
                            "</soap12:Body>"
                            "</soap12:Envelope>",_phoneText.text, @""];
    }
    else{
    
    
    
        [self showAlertView:@"输入手机号码"];
        return;
    }
        
        NSString *soapLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMsg length]];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
        [manager.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];
        NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx" parameters:nil];
        
        [self showIndicatorView:kNetworkConnecting];
        [request setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
        AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            [self dismissIndicatorView];
            NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];
            GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc] initWithXMLString:response options:0 error:nil];
            
            GDataXMLElement *xmlEle =[xmlDoc rootElement];
            
            NSArray *array = [xmlEle children];
            NSLog(@"array  count : %lu", (unsigned long)[array count]);
            
            for (int i = 0; i < [array count]; i++) {
                GDataXMLElement *ele = [array objectAtIndex:i];
                
                NSLog(@"childCount --> %lu",   (unsigned long)[ele childCount]);
                
                //            NSLog(@"children --> %@", [ele children]);
                NSLog(@"name --> %@",  [ele name]);
                NSLog(@"attributeForName --> %@", [[ele attributeForName:@"getMobileCodeInfoResult"] stringValue]);
                
                // 直接读标签间的String
                NSLog(@"stringValue --> %@", [ele stringValue]);
                
                
                [self dismissIndicatorView];
                [self showAlertView:[ele stringValue]];
                
            }
   
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];
            NSLog(@"%@, %@", operation, error);
        }];
        [manager.operationQueue addOperation:operation];
   
        
    }
    @end
